package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.constant.RedisMessageConstant;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Order;
import com.itheima.health.service.OrderService;
import com.itheima.health.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderMobileController {

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private OrderService orderService;

    // 体检预约
    @RequestMapping("/submit")
    public Result submit(@RequestBody Map<String, String> map) {
        // 获取手机号和验证码（页面）
        String validateCode = map.get("validateCode");
        // 通过手机号从Redis中获取验证码
        String code4 = jedisPool.getResource().get(map.get("telephone") + RedisMessageConstant.SENDTYPE_ORDER);
        //对手机号输入验证码做校验，页面输入的验证码和Redis中存放的验证码是否一致
        if (code4 == null || !code4.equals(validateCode)) {
            // 如果不一致，提示【验证码输入有误】
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }
        Result result = null;
        try {
            //传递参数OrderInfo，设置map的值orderType，表示微信预约，给Service处理（Service进行校验）
            map.put("orderType", Order.ORDERTYPE_WEIXIN);
            result = orderService.order(map);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result;
        }
    }

    // 使用订单id，查询订单的详情
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Map<String,Object> map = orderService.findById(id);
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }
    }
}
